package com.sqllog.admin.sql.controller;

import com.github.pagehelper.PageInfo;
import com.sqllog.admin.auth.domain.response.Result;
import com.sqllog.admin.auth.domain.response.module.ResultTable;
import com.sqllog.admin.framework.controller.ResultController;
import com.sqllog.admin.sql.domain.QueryParamInfo;
import com.sqllog.admin.sql.domain.SqlChangeEntity;
import com.sqllog.admin.sql.domain.SqlLogInfo;
import com.sqllog.admin.sql.service.CanalServerLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * TODO
 *
 * @author chennengcheng
 * @date 2022-01-23 15:32
 * @remark
 */

@RestController
@RequestMapping("/sqlLog/")
public class CanalServerLogController extends ResultController {

    /**
     * Describe: 基础路径
     */
    private static String MODULE_PATH = "sqlLog/";

    @Autowired
    private CanalServerLogService canalServerLogService;


    @GetMapping("main")
    public ModelAndView main(ModelAndView modelAndView) {
        return jumpPage(MODULE_PATH + "main");
    }

    @GetMapping("data")
    public ResultTable data(QueryParamInfo queryParamInfo) {
        PageInfo<SqlLogInfo> sqlLogInfoPageInfo = canalServerLogService.findPage(queryParamInfo);
        return pageTable(sqlLogInfoPageInfo.getList(), sqlLogInfoPageInfo.getTotal());
    }

    @GetMapping("detail")
    public ModelAndView detail(Model model, QueryParamInfo queryParamInfo) {
        model.addAttribute("collectionName", queryParamInfo.getCollectionName());
        model.addAttribute("id", queryParamInfo.getId());
        return jumpPage(MODULE_PATH + "detail");
    }

    @GetMapping("getById")
    public Result getById(QueryParamInfo queryParamInfo) {
        SqlChangeEntity sqlChangeEntity = canalServerLogService.getById(queryParamInfo.getId(), queryParamInfo.getCollectionName());
        return success(sqlChangeEntity);
    }

}
